home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / 3Dfx / demos / paltex.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  4KB  |  157 lines

  1. /* paltex.c */
  2.  
  3. /*
  4.  * Paletted texture demo.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/glut.h>
  11.  
  12.  
  13. static float Rot = 0.0;
  14.  
  15.  
  16. static void Idle( void )
  17. {
  18.    Rot += 5.0;
  19.    glutPostRedisplay();
  20. }
  21.  
  22.  
  23. static void Display( void )
  24. {
  25.    glClear( GL_COLOR_BUFFER_BIT );
  26.  
  27.    glPushMatrix();
  28.    glRotatef(Rot, 0, 0, 1);
  29.  
  30.    glBegin(GL_POLYGON);
  31.    glTexCoord2f(0, 1);  glVertex2f(-1, -1);
  32.    glTexCoord2f(1, 1);  glVertex2f( 1, -1);
  33.    glTexCoord2f(1, 0);  glVertex2f( 1,  1);
  34.    glTexCoord2f(0, 0);  glVertex2f(-1,  1);
  35.    glEnd();
  36.  
  37.    glPopMatrix();
  38.  
  39.    glutSwapBuffers();
  40. }
  41.  
  42.  
  43. static void Reshape( int width, int height )
  44. {
  45.    glViewport( 0, 0, width, height );
  46.    glMatrixMode( GL_PROJECTION );
  47.    glLoadIdentity();
  48.    glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  49.    glMatrixMode( GL_MODELVIEW );
  50.    glLoadIdentity();
  51.    glTranslatef( 0.0, 0.0, -15.0 );
  52. }
  53.  
  54.  
  55. static void Key( unsigned char key, int x, int y )
  56. {
  57.    switch (key) {
  58.       case 27:
  59.          exit(0);
  60.          break;
  61.    }
  62.    glutPostRedisplay();
  63. }
  64.  
  65.  
  66. static void SpecialKey( int key, int x, int y )
  67. {
  68.    switch (key) {
  69.       case GLUT_KEY_UP:
  70.          break;
  71.       case GLUT_KEY_DOWN:
  72.          break;
  73.       case GLUT_KEY_LEFT:
  74.          break;
  75.       case GLUT_KEY_RIGHT:
  76.          break;
  77.    }
  78.    glutPostRedisplay();
  79. }
  80.  
  81.  
  82. static void Init( void )
  83. {
  84.    GLubyte texture[8][8] = {  /* PT = Paletted Texture! */
  85.       {  0,   0,   0,   0,   0,   0,   0,   0},
  86.       {  0, 100, 100, 100,   0, 180, 180, 180},
  87.       {  0, 100,   0, 100,   0,   0, 180,   0},
  88.       {  0, 100,   0, 100,   0,   0, 180,   0},
  89.       {  0, 100, 100, 100,   0,   0, 180,   0},
  90.       {  0, 100,   0,   0,   0,   0, 180,   0},
  91.       {  0, 100,   0,   0,   0,   0, 180,   0},
  92.       {  0, 100, 255,   0,   0,   0, 180, 250},
  93.    };
  94.  
  95.    GLubyte table[256][4];
  96.    int i;
  97.  
  98.    if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
  99.       printf("Sorry, GL_EXT_paletted_texture not supported\n");
  100.       exit(0);
  101.    }
  102.  
  103.    /* put some wacky colors into the texture palette */
  104.    for (i=0;i<256;i++) {
  105.       table[i][0] = i;
  106.       table[i][1] = 0;
  107.       table[i][2] = 127 + i / 2;
  108.       table[i][3] = 255;
  109.    }
  110.  
  111. #ifdef GL_EXT_paletted_texture
  112.    glColorTableEXT(GL_TEXTURE_2D,    /* target */
  113.                    GL_RGBA,          /* internal format */
  114.                    256,              /* table size */
  115.                    GL_RGBA,          /* table format */
  116.                    GL_UNSIGNED_BYTE, /* table type */
  117.                    table);           /* the color table */
  118.  
  119.    glTexImage2D(GL_TEXTURE_2D,       /* target */
  120.                 0,                   /* level */
  121.                 GL_COLOR_INDEX8_EXT, /* internal format */
  122.                 8, 8,                /* width, height */
  123.                 0,                   /* border */
  124.                 GL_COLOR_INDEX,      /* texture format */
  125.                 GL_UNSIGNED_BYTE,    /* texture type */
  126.                 texture);            /* teh texture */
  127. #endif
  128.  
  129.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  130.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  131.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  132.    glEnable(GL_TEXTURE_2D);
  133. }
  134.  
  135.  
  136. int main( int argc, char *argv[] )
  137. {
  138.    glutInit( &argc, argv );
  139.    glutInitWindowPosition( 0, 0 );
  140.    glutInitWindowSize( 640, 480 );
  141.  
  142.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  143.  
  144.    glutCreateWindow(argv[0]);
  145.  
  146.    Init();
  147.  
  148.    glutReshapeFunc( Reshape );
  149.    glutKeyboardFunc( Key );
  150.    glutSpecialFunc( SpecialKey );
  151.    glutDisplayFunc( Display );
  152.    glutIdleFunc( Idle );
  153.  
  154.    glutMainLoop();
  155.    return 0;
  156. }
  157.